home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 648 < prev    next >
Encoding:
Text File  |  1996-08-06  |  4.1 KB  |  109 lines

  1. Path: locutus.rchland.ibm.com!usenet
  2. From: pstaite@vnet.ibm.com
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Passing C++ member functions to Xview notifier functions.
  5. Date: 5 Jan 1996 15:16:54 GMT
  6. Organization: IBM OS/2 Device Driver Development  Rochester, MN
  7. Message-ID: <4cjfd6$16vn@locutus.rchland.ibm.com>
  8. References: <4chhsn$sim@msunews.cl.msu.edu>
  9. Reply-To: pstaite@vnet.ibm.com
  10. NNTP-Posting-Host: warpone.rchland.ibm.com
  11. X-Newsreader: IBM NewsReader/2 v1.2
  12.  
  13. In <4chhsn$sim@msunews.cl.msu.edu>, Robert Foster <rkf> writes:
  14. >Greetings,
  15. >
  16. >I am using C++ classes in an Xview based program on a Sun/Solaris platform.
  17. >
  18. >When I try to do the following within a class member function...
  19. >
  20. >notify_set_input_func( client1, Pipe::read_from, pipe_in_out[1][0] ) ;
  21. >
  22. >...the compiler complains about the Pipe::read_from in the following way...
  23.  
  24. Next *time* spare our *time* and read the FAQ first:
  25.  
  26. ==============================================================================
  27. SECTION 18: Pointers to member functions
  28. ==============================================================================
  29.  
  30. Q111: Is the type of "ptr-to-member-fn" different from "ptr-to-fn"?
  31.  
  32. Yep.
  33.  
  34. Consider the following function:
  35.  
  36.         int f(char a, float b);
  37.  
  38. If this is an ordinary function, its type is:    int (*)      (char,float);
  39. If this is a method of class Fred, its type is:  int (Fred::*)(char,float);
  40.  
  41. ==============================================================================
  42.  
  43. Q112: How do I pass a ptr to member fn to a signal handler, X event callback,
  44.    etc?
  45.  
  46. Don't.
  47.  
  48. Because a member function is meaningless without an object to invoke it on, you
  49. can't do this directly (if The X Windows System was rewritten in C++, it would
  50. probably pass references to OBJECTS around, not just pointers to fns; naturally
  51. the objects would embody the required function and probably a whole lot more).
  52.  
  53. As a patch for existing software, use a top-level (non-member) function as a
  54. wrapper which takes an object obtained through some other technique (held in a
  55. global, perhaps).  The top-level function would apply the desired member
  56. function against the global object.
  57.  
  58. E.g., suppose you want to call Fred::memfn() on interrupt:
  59.  
  60.         class Fred {
  61.         public:
  62.           void memfn();
  63.           static void staticmemfn();    //a static member fn can handle it
  64.           //...
  65.         };
  66.  
  67.         //wrapper fn remembers the object on which to invoke memfn in a global:
  68.         Fred* object_which_will_handle_signal;
  69.         void Fred_memfn_wrapper() { object_which_will_handle_signal->memfn(); }
  70.  
  71.         main()
  72.         {
  73.           /* signal(SIGINT, Fred::memfn); */   //Can NOT do this
  74.           signal(SIGINT, Fred_memfn_wrapper);  //Ok
  75.           signal(SIGINT, Fred::staticmemfn);   //Also Ok
  76.         }
  77.  
  78. Note: static member functions do not require an actual object to be invoked, so
  79. ptrs-to-static-member-fns are type compatible with regular ptrs-to-fns (see ARM
  80. ["Annotated Reference Manual"] p.25, 158).
  81.  
  82. ==============================================================================
  83.  
  84. Q113: Why do I keep getting compile errors (type mismatch) when I try to use a
  85.    member function as an interrupt service routine? 
  86.  
  87. This is a special case of the previous two questions, therefore read the
  88. previous two answers first.
  89.  
  90. Non-static member functions have a hidden parameter that corresponds to the
  91. 'this' pointer.  The 'this' pointer points to the instance data for the
  92. object.  The interrupt hardware/firmware in the system is not capable of
  93. providing the 'this' pointer argument.  You must use "normal" functions (non
  94. class members) or static member functions as interrupt service routines.
  95.  
  96. One possible solution is to use a static member as the interrupt service
  97. routine and have that function look somewhere to find the instance/member pair
  98. that should be called on interrupt.  Thus the effect is that a normal method
  99. is invoked on an interrupt, but for technical reasons you need to call an
  100. intermediate function first.
  101.  
  102. ==============================================================================
  103.  
  104.  
  105.  
  106. Phil Staite, team OS/2
  107. internet: pstaite@vnet.ibm.com  internal: pstaite@rchland
  108.  
  109.